import axios from "axios";
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
import ElMessage from "@/components/ElMessage.js";
import { getToken } from "@/stores/modules/home";
class Request {
  static request: any = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_URL,
    // baseURL: "http://localhost:8080",
  });
  static requestInterceptors() {
    this.request.interceptors.request.use((config: any) => {
      config.headers["token"] = getToken() || "";
      return config;
    });
  }
  static responseInterceptors() {
    this.request.interceptors.response.use(
      (res: any) => {
        if (res.data.code !== 0) {
          ElMessage({
            type: "error",
            msg: res.data.msg,
          });
        }
        return res.data;
      },
      (reject: any) => {
        console.log("-=-=");
        ElMessage({ type: "error", msg: "网络错误，请联系管理员" });
      }
    );
  }
}
Request.requestInterceptors();
Request.responseInterceptors();
export default Request.request;
